<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书管理</title>
</head>
<link rel="stylesheet" href="./lib/bootstrap.css" />
<script src="./lib/jquery.js"></script>
</head>

<body style="padding: 15px;">
    <!-- 添加图书的Panel面板 -->
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加新图书</h3>
        </div>
        <div class="panel-body form-inline">

            <div class="input-group">
                <div class="input-group-addon">书名</div>
                <input type="text" class="form-control" id="iptBookname" placeholder="请输入书名">
            </div>

            <div class="input-group">
                <div class="input-group-addon">作者</div>
                <input type="text" class="form-control" id="iptAuthor" placeholder="请输入作者">
            </div>

            <div class="input-group">
                <div class="input-group-addon">出版社</div>
                <input type="text" class="form-control" id="iptPublisher" placeholder="请输入出版社">
            </div>

            <button id="btnAdd" class="btn btn-primary">添加</button>

        </div>
    </div>


    <!-- 图书的表格 -->
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>Id</th>
                <th>书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody id="tb"></tbody>
    </table>
    <script>
        function getBookList() {
            $.get('http://www.liulongbin.top:3006/api/getbooks', function(res) {
                if (res.status !== 200) return alert('获取数据失败！')

                var rows = []
                $.each(res.data, function(i, item) {
                    rows.push('<tr><td>' + item.id + '</td><td>' + item.bookname + '</td><td>' + item.author + '</td><td>' + item.publisher + '</td><td><a href="javascript:;" class="del" data-id="' + item.id + '">删除</a></td></tr>')
                })
                $('#tb').empty().append(rows.join(''))
            })
        }
        getBookList()



        // 2.删除数据
        $('#tb').on('click', '.del', function() {
                // 弹出询问框
                var result = confirm('确定要删除吗?')


                if (result) {
                    // 获取id
                    var id = $(this).attr('data-id')
                    $.ajax({
                        type: 'get',
                        url: 'http://www.liulongbin.top:3006/api/delbook',
                        data: {
                            id: id
                        }, // 删除当前id数据
                        success: function(res) {
                            if (res.status !== 200) {
                                return alert('删除失败!')
                            }
                            getBookList()
                        }
                    })
                }
            })
            // 2.删除数据
        $('#tb').on('click', '.del', function() {
                // 弹出询问框
                var result = confirm('确定要删除吗?')


                if (result) {
                    // 获取id
                    var id = $(this).attr('data-id')
                    $.ajax({
                        type: 'get',
                        url: 'http://www.liulongbin.top:3006/api/delbook',
                        data: {
                            id: id
                        }, // 删除当前id数据
                        success: function(res) {
                            if (res.status !== 200) {
                                return alert('删除失败!')
                            }
                            getBookList()
                        }
                    })
                }

            })
            //
        $('#btnAdd').on('click', function() {
            // 收集数据（名称、作者、出版社）
            var bookname = $('#iptBookname').val().trim();
            var author = $('#iptAuthor').val().trim();
            var publisher = $('#iptPublisher').val().trim();

            // 判断是否为空
            if (bookname.length <= 0 || author.length <= 0 || publisher.length <= 0) {
                return alert('信息不完整')
            }
            // 发送ajax请求，添加图书
            $.ajax({
                type: 'post',
                url: 'http://www.liulongbin.top:3006/api/addbook',
                data: {
                    bookname: bookname,
                    author: author,
                    publisher: publisher
                },
                success: function(res) {
                    // console.log(res)
                    // 判断
                    if (res.status !== 201) {
                        return alert(res.message)
                    }
                    // 重新获取图书，重新渲染
                    getBookList();
                    // 清空表单输入框
                    $('#iptBookname').val('')
                    $('#iptAuthor').val('')
                    $('#iptPublisher').val('')
                }
            })

        })
    </script>
</body>

</html>